// pages/component/homes/bindbankcard/bindbankcard.js
var GlobalData = require('../../../../utils/GlobalData.js');
var dataManager = require('../../../../utils/dataManager.js');
var bindBankCard = require('../../../../utils/bindBankCard.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    state: false,
    button: "绑定银行卡",
    /**输入登录密码 */
    pswd: "",
    isShow: false,
    mobile: 0,
    noData: false,
    bindInfoData: { bankName: "", account: "", trueName: "", branch: "" },
    isShowBankCardInfo: false,
    isHasData: false,
    sourceUrl: ""


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.url) {
      this.data.sourceUrl = options.url
    }
    if (GlobalData.homeData) {
      this.data.bindInfoData.trueName = GlobalData.homeData.truename;
    } else
      this.data.bindInfoData.trueName = "";
      this.setData({
        bindInfoData: this.data.bindInfoData
      })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.dealWithInfo()
  },
  /**获取到银行卡信息后进行处理 */
  dealWithInfo() {
    if (GlobalData.bindInfo.length) {
      for (let i = 0; i < GlobalData.bindInfo.length; i++) {
        if (GlobalData.bindInfo[i].type == 1) {
          this.data.bindInfoData = GlobalData.clone(GlobalData.bindInfo[i])
          if (this.data.bindInfoData.bankName.indexOf("-") > -1) {
            let arr = this.data.bindInfoData.bankName.split("-")
            this.data.bindInfoData.bankName = arr[0];
            this.data.bindInfoData.branch = arr[1];
          } else {
            this.data.bindInfoData.branch = "";
          }
          this.setData({
            bindInfoData: this.data.bindInfoData,
            state: true,
            isHasData: true,
            button: "更换银行卡"
          });
          break;
        }
      }
    }
  },
  inputName: function (e) {
    console.log(e.detail.value)
    let name = e.detail.value.trim();
    this.data.bindInfoData.trueName = name
    this.setData({
      bindInfoData: this.data.bindInfoData
    })
  },
  inputBankCard: function (e) {
    var that = this;
    console.log(e.detail.value)
    this.data.bindInfoData.account = e.detail.value
    this.setData({
      bindInfoData: this.data.bindInfoData
    })
    bindBankCard.getBankBin(e.detail.value, function (err, data) {
      console.log(data, err, "获取银行卡信息")
      if (data) {
        that.data.bindInfoData.bankName = data.bankName;
        that.setData({
          bindInfoData: that.data.bindInfoData
        })
      }
      if (err)
        app.onshowModal(err)
    })

  },
  inputCardType: function (e) {
    console.log(e.detail.value)
    this.data.bindInfoData.bankName = e.detail.value.trim();
    this.setData({
      bindInfoData: this.data.bindInfoData
    })
  },
  inputSubBranch: function (e) {
    console.log(e.detail.value)
    this.data.bindInfoData.branch = e.detail.value.trim();
    this.setData({
      bindInfoData: this.data.bindInfoData
    })
  },
  onClose() {
    this.setData({
      isShow: false,
      isShowBankCardInfo: false
    })
  },

  onConfirmClick() {
    console.log(this.data.bindInfoData)
    // if (!this.data.bindInfoData.account || !this.data.bindInfoData.bankName || !this.data.bindInfoData.trueName || !this.data.bindInfoData.branch) {
    //   app.onshowModal("请输入完整的信息。")
    //   return;
    // }
    if (this.data.button == "绑定银行卡") {
      this.onBindInfo();
    } else if (this.data.button == "更换银行卡") {
      this.setData({
        isShow: true
      })
    }
  },
  //更换银行卡
  //更换银行卡
  changeBank() {
    this.data.bindInfoData.account = "";
    if (GlobalData.homeData) {
      this.data.bindInfoData.trueName = GlobalData.homeData.truename;
    } else
      this.data.bindInfoData.trueName = "";
    this.data.bindInfoData.bankName = "";
    this.data.button = "绑定银行卡";
    this.data.bindInfoData.branch = ""
    this.data.state = !this.state;
    this.data.isHasData = false;
    this.setData({
      bindInfoData: this.data.bindInfoData,
      button: this.data.button,
      state: this.data.state,
      isHasData: this.data.isHasData,
    })
  },

  onBindInfoComplete(data) {
    dataManager.OnRequestPost({ method: "getbankaccountlist", "token": GlobalData.token }, this.onWithdrawComplete);
  },
  onWithdrawComplete(data) {
    var that = this;
    GlobalData.bindInfo = data;
    app.onshowModal("绑定成功");

    that.dealWithInfo()

    wx.showModal({
      title: '提示',
      content: "绑定成功",
      showCancel: false,
      success: function (res) {
        if (that.data.sourceUrl)
          wx.navigateBack({
            delta: 1
          })
      }
    })

  },

  onInformation() {
    this.setData({
      isShowBankCardInfo: false
    })
    if (!this.data.bindInfoData.account || !this.data.bindInfoData.bankName || !this.data.bindInfoData.trueName || !this.data.bindInfoData.branch) {
      app.onshowModal("请输入完整的信息。")
      return;
    }
    let bankname = this.data.bindInfoData.bankName + "-" + this.data.bindInfoData.branch;
    let url = { method: "bindbankaccount", token: GlobalData.token, type: 1, bankname: bankname, truename: this.data.bindInfoData.trueName, account: this.data.bindInfoData.account }
    dataManager.OnRequestPost(url, this.onBindInfoComplete);
  },

  onBindInfo() {
    if (!this.data.bindInfoData.account || !this.data.bindInfoData.bankName || !this.data.bindInfoData.trueName || !this.data.bindInfoData.branch) {
      app.onshowModal("请输入完整的信息。")
      return;
    }
    this.setData({
      isShowBankCardInfo: true
    })
  },
  onFindPswd() {

    dataManager.UseNavigateTo("/pages/component/logins/forgetPassword/forgetPassword", false)
  },
  onInputPassWord(e) {
    this.setData({
      pswd: e.detail.value.trim()
    })
  },

  //验证登录密码
  onConfirm() {
    if (this.data.pswd == "" || this.data.pswd == null) {
      app.onshowModal("请输入登录密码。");
      return;
    }
    try {
      dataManager.OnRequestPost({ method: "validatepassword", "token": GlobalData.token, password: this.data.pswd }, this.validatepasswordComplete);
    } catch (err) {
      app.onshowModal(err);
    }

  },
  validatepasswordComplete(data) {
    this.setData({
      isShow: false,
      pswd: ""
    })
    // this.app.show("提示", pkg.errmsg, "确认");
    this.changeBank();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})